#-*-encoding=utf-8-*-
@script_attr(permission='zope.Public', use_template='basic')
def index():
    """会议室占用图

    Meeting room usage query by date.
    """

    year = int(request.get('year', 0))
    month = int(request.get('month', 0))
    day = int(request.get('day', 0))
    if year == 0 or month == 0 or day == 0:
        today = datetime.date.today()
        year = today.year
        month = today.month
        day = today.day
    
    flow_url = absoluteURL(context, request) + '/++script++zopen.meeting.index'
    
    cal = calendar.monthcalendar(year, month)
    l = datetime.datetime(year, month, 1, 0, 0, 0)  - datetime.timedelta(10)
    l_year, l_month = l.year, l.month
    n = datetime.datetime(year, month, 1, 0, 0, 0)  + datetime.timedelta(32)
    n_year, n_month = n.year, n.month
    
    queried_day = datetime.datetime(year, month, day, 0, 0, 0)
    queried_day_next = queried_day + datetime.timedelta(1)
    
    rooms = IFieldStorage(context)['rooms']
    results=  searchObjects(path=context,object_provides=['zopen.flow.interfaces.IFlowSheet'], 
                            subjects=["Booked", "Finished"], start=(None, queried_day_next), 
                             end=(queried_day, None), restricted = False)
    
    room_usage = {}
    sheet_datas = {}
    for room in rooms:
        room_usage[room] = set()
        sheet_datas[room] = {}
    
    for flowsheet in results:
        creator = flowsheet['creators'][0]
        creator = org_info.getPrincipalInfo(creator)['title']
    
        start_dt = flowsheet['start']
        end_dt = flowsheet['end']
        booked_room = str(flowsheet['room'])
    
        start_offset = start_dt.hour * 2
        if start_dt.minute >= 30:
            start_offset = start_offset + 1
    
        end_offset = end_dt.hour * 2
        if end_dt.minute >= 30:
            end_offset = end_offset + 1
        elif end_dt.minute == 0:
            end_offset = end_offset - 1
    
        book_time = range(start_offset, end_offset+1)
    
        sheet_datas[booked_room].setdefault(book_time[0], []).append({
            'title':flowsheet['title'],
            'start': start_dt,
            'end': end_dt,
            'url': absoluteURL(flowsheet, request),
            'creator': creator
        })
    
    
        book_time = set(book_time)
    
        if booked_room not in room_usage:
            room_usage[booked_room] = book_time
        else:
            room_usage[booked_room] = room_usage[booked_room] | book_time
    
    
    return render_template(u'meeting_cal', year=year, month=month, day=day, cal = cal,
            rooms = rooms, n_year = n_year, n_month = n_month, l_year = l_year, l_month=l_month, flow_url=flow_url,
            room_usage = room_usage, sheet_datas=sheet_datas
        ).decode('utf-8')
    

@script_attr(permission='zope.View', use_template='none')
def setup():
    """安装程序

这是应用的安装程序"""

    dic = deployDataManager(context, 'meetingroom', '会议室预订', 'zopen.meeting.booking', 'zopen.meeting.meeting')
    
    IAppletData(dic).default_view='zopen.meeting.index'
    
    return request.response.redirect(absoluteURL(dic, request))
    

